Skip to content

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883)#2029

Draft
Ashutosh0x wants to merge 5 commits intomodelcontextprotocol:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertion
Draft

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883)#2029
Ashutosh0x wants to merge 5 commits intomodelcontextprotocol:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertion

Conversation

@Ashutosh0x
Copy link

When Starlette's BaseHTTPMiddleware wraps the SSE endpoint, it expects http.response.body messages. However, the SSE handler sends raw ASGI events, causing 'AssertionError: Unexpected message'.

This PR fixes the issue by replacing the Starlette endpoint wrapper (sse_endpoint function) with a callable ASGI class (HandleSseAsgi). This ensures the SSE handler bypasses middleware response body wrapping, resolving the AssertionError.

Also removes the unnecessary 'return Response()' from handle_sse since it's a raw ASGI callable.

Closes #883

…ssertionError (modelcontextprotocol#883)

When Starlette's BaseHTTPMiddleware wraps the SSE endpoint, it expects
http.response.body messages. However, the SSE handler sends raw ASGI
events, causing 'AssertionError: Unexpected message'.

Fix: Replace the Starlette endpoint wrapper (sse_endpoint function)
with a callable ASGI class (HandleSseAsgi) so the SSE handler bypasses
middleware response body wrapping. Also remove the unnecessary
'return Response()' from handle_sse since it's a raw ASGI callable.

Closes modelcontextprotocol#883
@Ashutosh0x Ashutosh0x mentioned this pull request Feb 10, 2026
@maxisbey maxisbey added the bug Something isn't working label Feb 11, 2026
@maxisbey maxisbey marked this pull request as draft February 11, 2026 14:20
@maxisbey
Copy link
Contributor

Marking as draft until CI is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssertionError: Unexpected message while using middlewares

2 participants